function setItem(key: string, value: any, exp: number = 0): void {
  let expDate = 0;
  if (exp === 0) {
    expDate = 0;
  } else {
    expDate = Date.now() + exp * 1000;
  }
  localStorage.setItem(key, JSON.stringify({ value, exp: expDate }));
}

function getItem(key: string): any {
  const item = localStorage.getItem(key);
  // 判断item 是否是json字符串
  if (item && typeof item === "string" && item.startsWith("{")) {
    const { value, exp } = JSON.parse(item);
    if (exp === 0 || exp > Date.now()) {
      return value;
    }
  }
  return null;
}

function removeItem(key: string): void {
  localStorage.removeItem(key);
}

function clear(): void {
  localStorage.clear();
}

export { setItem, getItem, removeItem, clear };
